﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerOneProject : PlayerProjectBase
{
    void Awake()
    {
        _rb2d = this.GetComponent<Rigidbody2D>();
        _col2d = this.GetComponent<Collider2D>();
        dir = FaceDir.FACE_DIR_RIGHT;
    }

    /// <summary>
    /// 发射子弹，传入参数：初始速度，伤害
    /// </summary>
    public void InitProject(Vector2 velocity, int dmg)
    {
        this.velocity = velocity;
        this.dmg = dmg;

        float angle = Mathf.Atan(velocity.y / velocity.x) * Mathf.Rad2Deg;
        _rb2d.SetRotation(angle);

        Destroy(this.gameObject, 10f);
    }

    void FixedUpdate()
    {
        Vector2 pos = _rb2d.position;
        pos = pos + velocity * Time.fixedDeltaTime;
        _rb2d.MovePosition(pos);
    }

    //销毁子弹
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("BounceBack"))
        {
            collision.transform.GetComponent<BounceBackProjectSpawn>().Spawn(this.transform);
            Destroy(this.gameObject);
        }
        if (collision.CompareTag("ProjectDestroyer"))
        {
            Destroy(this.gameObject);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.CompareTag("Enemy"))
        {
            Destroy(this.gameObject);
        }
        else if (collision.transform.CompareTag("Player2"))
        {
            Destroy(this.gameObject);
        }
    }
}
